$(function () {
    $.ajax({
        url: "./goSaleBack/initPage", //请求的url地址
        xhrFields: {
            withCredentials: true
        },
        dataType: "json", //返回格式为json
        type: "POST", //请求方式
        processData: false, //对表单data数据是否进行序列化
        contentType: false, //dataType设置你收到服务器数据的格式
        headers: {
            'Content-Type': 'application/json'
        },
        beforeSend: function() {
            //请求前的处理
        },
        success: function(data) {
            if(data.status==0){
                // top.location.href = "../../login.html";
                window.location.href="../../login.html";
            }
            if(data.status=="1"){
                var data_json = JSON.parse(data.data);
                $("#StoreSelect").empty();

                $("#PostSelect").append("<option selected=\"selected\" value=\"\">选择岗位</option>");
                $("#SaleManSelect").append("<option selected=\"selected\" value=\"\">选择姓名</option>");
                $("#CategorySelect").append("<option selected=\"selected\" value=\"\">选择类别</option>");
                $("#ProjectSelect").append("<option selected=\"selected\" value=\"\">选择项目</option>")
                $("#StandardSelect").append("<option selected=\"selected\" value=\"\">选择岗位标准</option>")
                if(data.store=="总部"){
                    $("#StoreSelect").append("<option value=''>选择中心/店面</option>");
                    for(var i=0;i<data_json.data.length;i++){
                        $("#StoreSelect").append("<option value='"+data_json.data[i].Post+"'>"+data_json.data[i].Post+"</option>");
                    }
                }else{

                    $("#StoreSelect").append("<option value=\""+data.store+"\">"+data.store+"</option>")
                    $("#StoreSelect").append("<option selected=\"selected\" value=''>选择中心/店面</option>");
                }



            }
        },
        complete: function() {
            //请求完成的处理
        },
        error: function() {}
    });
})

//查询销售部岗位下拉
function getPostSelect() {
    var store = $("#StoreSelect").val();
    var jsondata = {
        "Store":store
    }
    var data_json = JSON.stringify(jsondata);
    $.ajax({
        url: "./goSaleBack/queryAllSalePostSelect", //请求的url地址
        xhrFields: {
            withCredentials: true
        },
        dataType: "json", //返回格式为json
        type: "POST", //请求方式
        data:data_json,
        processData: false, //对表单data数据是否进行序列化
        contentType: false, //dataType设置你收到服务器数据的格式
        headers: {
            'Content-Type': 'application/json'
        },
        beforeSend: function() {
            //请求前的处理
        },
        success: function(data) {
            if(data.status==0){
                // top.location.href = "../../login.html";
                window.location.href="../../login.html";
            }
            if(data.code==200){
                var data_json = JSON.parse(data.data);
                $("#PostSelect").empty();
                $("#PostSelect").append("<option selected=\"selected\" value=\"\">选择岗位</option>")
                for(var i=0;i<data_json.data.length;i++){
                    $("#PostSelect").append("<option value='"+data_json.data[i].Post+"'>"+data_json.data[i].Post+"</option>");
                }

            }
        },
        complete: function() {
            //请求完成的处理
        },
        error: function() {}
    });
}

//查询销售部岗位人员信息下拉
function getSaleManSelect() {
    var store = $("#StoreSelect").val();
    var post = $("#PostSelect").val();
    var jsondata = {
        "Store":store,
        "Post":post
    }
    var data_json = JSON.stringify(jsondata);
    $.ajax({
        url: "./goSaleBack/queryAllSaleManSelect", //请求的url地址
        xhrFields: {
            withCredentials: true
        },
        dataType: "json", //返回格式为json
        type: "POST", //请求方式
        data:data_json,
        processData: false, //对表单data数据是否进行序列化
        contentType: false, //dataType设置你收到服务器数据的格式
        headers: {
            'Content-Type': 'application/json'
        },
        beforeSend: function() {
            //请求前的处理
        },
        success: function(data) {
            if(data.status==0){
                // top.location.href = "../../login.html";
                window.location.href="../../login.html";
            }
            if(data.code==200){
                var data_json = JSON.parse(data.data);
                $("#SaleManSelect").empty();
                $("#SaleManSelect").append("<option selected=\"selected\" value=\"\">选择姓名</option>")
                for(var i=0;i<data_json.data.length;i++){
                    $("#SaleManSelect").append("<option value='"+data_json.data[i].UserNo+"'>"+data_json.data[i].Name+"</option>");
                }

            }
        },
        complete: function() {
            //请求完成的处理
        },
        error: function() {}
    });
}

//通过岗位、店面查询具有的类别下拉
function searchCategory() {
    var store = $("#StoreSelect").val();
    var post = $("#PostSelect").val();
    var UserId = $("#SaleManSelect").val();
    if(UserId==null||UserId==''){
        return false;
    }
    var jsondata = {
        "Store":store,
        "Post":post
    }
    var data_json = JSON.stringify(jsondata);
    $.ajax({
        url: "./goSaleBack/queryAccessCategory", //请求的url地址
        xhrFields: {
            withCredentials: true
        },
        dataType: "json", //返回格式为json
        type: "POST", //请求方式
        data:data_json,
        processData: false, //对表单data数据是否进行序列化
        contentType: false, //dataType设置你收到服务器数据的格式
        headers: {
            'Content-Type': 'application/json'
        },
        beforeSend: function() {
            //请求前的处理
        },
        success: function(data) {
            if(data.status==0){
                // top.location.href = "../../login.html";
                window.location.href="../../login.html";
            }
            if(data.code==200){
                var data_json = JSON.parse(data.data);
                data_json = JSON.parse(data_json.data)
                $("#CategorySelect").empty();
                $("#CategorySelect").append("<option selected=\"selected\" value=\"\">选择类别</option>")
                for(var i=0;i<data_json.length;i++){
                    $("#CategorySelect").append("<option value='"+data_json[i].CID+"'>"+data_json[i].CName+"</option>");
                }

            }
        },
        complete: function() {
            //请求完成的处理
        },
        error: function() {}
    });
}

//通过岗位、店面、类别查询具有的项目下拉
function searchProject() {
    $("#ProjectSelect").empty();
    $("#ProjectSelect").append("<option selected=\"selected\" value=\"\">选择项目</option>")
    var store = $("#StoreSelect").val();
    var post = $("#PostSelect").val();
    var UserId = $("#SaleManSelect").val();
    var Category = $("#CategorySelect").val();
    if(UserId==null||UserId==''){
        return false;
    }
    var jsondata = {
        "Store":store,
        "Post":post,
        "CID":Category
    }
    var data_json = JSON.stringify(jsondata);
    $.ajax({
        url: "./goSaleBack/queryAccessProject", //请求的url地址
        xhrFields: {
            withCredentials: true
        },
        dataType: "json", //返回格式为json
        type: "POST", //请求方式
        data:data_json,
        processData: false, //对表单data数据是否进行序列化
        contentType: false, //dataType设置你收到服务器数据的格式
        headers: {
            'Content-Type': 'application/json'
        },
        beforeSend: function() {
            //请求前的处理
        },
        success: function(data) {
            if(data.status==0){
                // top.location.href = "../../login.html";
                window.location.href="../../login.html";
            }
            if(data.code==200){
                var data_json = JSON.parse(data.data);
                data_json = JSON.parse(data_json.data)
                $("#ProjectSelect").empty();
                $("#ProjectSelect").append("<option selected=\"selected\" value=\"\">选择项目</option>")
                for(var i=0;i<data_json.length;i++){
                    $("#ProjectSelect").append("<option value='"+data_json[i].PID+"'>"+data_json[i].PName+"</option>");
                }

            }
        },
        complete: function() {
            //请求完成的处理
        },
        error: function() {}
    });
}

//通过岗位、店面、类别、项目查询具有的岗位标准下拉
function searchStandard() {
    $("#StandardSelect").empty();
    $("#StandardSelect").append("<option selected=\"selected\" value=\"\">岗位标准</option>")
    var store = $("#StoreSelect").val();
    var post = $("#PostSelect").val();
    var UserId = $("#SaleManSelect").val();
    var Category = $("#CategorySelect").val();
    var Project = $("#ProjectSelect").val();
    if(UserId==null||UserId==''){
        return false;
    }
    var jsondata = {
        "Store":store,
        "Post":post,
        "CID":Category,
        "PID":Project
    }
    var data_json = JSON.stringify(jsondata);
    $.ajax({
        url: "./goSaleBack/queryAccessStandard", //请求的url地址
        xhrFields: {
            withCredentials: true
        },
        dataType: "json", //返回格式为json
        type: "POST", //请求方式
        data:data_json,
        processData: false, //对表单data数据是否进行序列化
        contentType: false, //dataType设置你收到服务器数据的格式
        headers: {
            'Content-Type': 'application/json'
        },
        beforeSend: function() {
            //请求前的处理
        },
        success: function(data) {
            if(data.status==0){
                // top.location.href = "../../login.html";
                window.location.href="../../login.html";
            }
            if(data.code==200){
                var data_json = JSON.parse(data.data);
                data_json = JSON.parse(data_json.data)
                $("#StandardSelect").empty();
                $("#StandardSelect").append("<option selected=\"selected\" value=\"\">岗位标准</option>")
                for(var i=0;i<data_json.length;i++){
                    $("#StandardSelect").append("<option value='"+data_json[i].ID+"'>"+data_json[i].Name+"</option>");
                }

            }
        },
        complete: function() {
            //请求完成的处理
        },
        error: function() {}
    });
}


var pageSize = 99999999;
var vm = new Vue({
    el: "#app",
    data:function(){
        return{
            editDataModalFlag:false,
            standardObject:{},
            // 在脚本中，我们在表格中定义的数据和表头都需要在这里进行绑定
            columns1: [
                /*{
                    title: 'ID',
                    key: 'ID',
                    align : 'center'
                },*/
                {
                    title: ' ',
                    key: 'RowID',
                    width:60,
                    align : 'center'
                },
                {
                    title: '中心店面',
                    key: 'Store',
                    width:80,
                    align : 'center',
                    // width:80,
                    // fixed: 'left'
                },
                {
                    title: '姓名',
                    key: 'SalerName',
                    // width:80,
                    width:80,
                    align : 'center',
                    // fixed: 'left'
                },
                {
                    title: '岗位',
                    key: 'Post',
                    width:80,
                    // width:95,
                    align : 'center',
                },
                {
                    title: '类别',
                    key: 'CName',
                    width:70,
                    // width:80,
                    align : 'center'
                },
                {
                    title: '项目',
                    key: 'PName',
                    // width:120,
                    align : 'center',
                    render: (h, params) => {
                        return h('div', [
                            h('span', {
                                style: {
                                    display: 'inline-block',
                                    width: '100%',
                                    overflow: 'hidden',
                                    textOverflow: 'ellipsis',
                                    whiteSpace: 'nowrap'
                                },
                                domProps: {
                                    title: params.row.PName
                                }
                            }, params.row.PName)
                        ]);

                    }
                },
                {
                    title: '岗位标准',
                    key: 'SName',
                    // width:200,
                    align : 'center',
                    render: (h, params) => {
                        return h('div', [
                            h('span', {
                                style: {
                                    display: 'inline-block',
                                    width: '100%',
                                    overflow: 'hidden',
                                    textOverflow: 'ellipsis',
                                    whiteSpace: 'nowrap'
                                },
                                domProps: {
                                    title: params.row.SName
                                }
                            }, params.row.SName)
                        ]);

                    }
                },
                {
                    title: '基数(元)',
                    key: 'Base',
                    width:65,
                    // width:86,
                    align : 'center',
                },
                {
                    title: '占比(%)',
                    // width:70,
                    key: 'Proportion',
                    width:65,
                    align : 'center',
                },
                {
                    title: '金额(元)',
                    // width:80,
                    key: 'Money',
                    width:65,
                    align : 'center',
                },
                {
                    title: '完成率(%)',
                    // width:80,
                    key: 'FinishRate',
                    width:65,
                    align : 'center',
                },
                {
                    title: '实际工资(元)',
                    key: 'FactMoney',
                    width:70,
                    // width:100,
                    align : 'center',
                },
                {
                    title: '操作',
                    key: '',
                    // width: 60,
                    align: 'center',
                    width:110,
                    // fixed:"right",
                    render: (h, params) => {
                        return h('div', [
                            h('Button', {
                                props: {
                                    id:'xiugaibutton',
                                    type: 'primary',
                                    size: 'small'
                                },
                                style: {
                                    border:'#7B7B7B',
                                    background:'#7B7B7B',
                                    marginBottom:'1px',
                                    marginRight: '5px'
                                },
                                on: {
                                    click: () => {
                                        this.edit(params)
                                    }
                                }
                            }, '修改'),
                            h('Button', {
                                props: {
                                    id:'shanchubutton',
                                    type: 'error',
                                    size: 'small'
                                },
                                style: {
                                    border:'#7B7B7B',
                                    background:'#7B7B7B',
                                    marginRight: '5px'
                                },
                                on: {
                                    click: () => {
                                        this.deleteAss(params)
                                    }
                                }
                            }, '删除'),
                        ]);

                    }
                }
            ],
            // 接下来绑定数据，分别对应前面的列的key值来进行数据绑定
            historyData: [],
            // 初始化信息总条数
            dataCount:0,
            stripe:true
        }
    },

    methods: {
        deleteAss (params) {
            this.$Modal.confirm({
                title: '是否确定删除',
                content: '<p></p><p>确定删除？</p>',
                onOk: () => {
                    this.remove(params);
                    this.$Message.info('删除成功');

                },
                onCancel: () => {
                    this.$Message.info('取消删除');
                }
            });
        },
        querytable(pageNum,pageSize){
            var info ;
            var total;
            var AssessStartDate = $("#time1").val();
            var AssessEndDate = $("#time2").val();
            var Store = $("#StoreSelect").val();
            if(Store==""||Store==null){
                Store = "";
            }
            var Post = $("#PostSelect").val();
            if(Post==""||Post==null){
                Post = "";
            }
            var SalerID = $("#SaleManSelect").val();
            if(SalerID==""||SalerID==null){
                SalerID = "";
            }
            var CID = $("#CategorySelect").val();
            if(CID==""||CID==null){
                CID = "";
            }
            var PID = $("#ProjectSelect").val();
            if(PID==""||PID==null){
                PID = "";
            }
            var SID = $("#StandardSelect").val();
            if(SID==""||SID==null){
                SID = "";
            }
            var jsondata={"AssessStartDate":AssessStartDate,"AssessEndDate":AssessEndDate,"Store": Store,"Post":Post,"SalerID":SalerID,"CID":CID,"PID":PID,"SID":SID,"page":pageNum, "row":pageSize};
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goSaleBack/queryAllAssess", //请求的url地址
                dataType: "json", //返回格式为json
                async:false,
                data: data_json, //参数值
                type: "POST", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                success: function(data) {
                    info = eval(data.data);
                    total = data.totalnum;
                },
                error: function() {},
            });
            if(info){
                this.historyData = info;
                this.dataCount = total;
            }
        },
        // 2 分页
        changepage(index){
            this.querytable(index,pageSize);
        },
        //3 修改
        edit(params){
            if(params.row.Store=="总计"||params.row.Store=="当前页合计"){
                return false;
            }else{
                var self = this
                self.editDataModalFlag = true
                self.standardObject = params.row;
            }
        },
        //3 删除
        remove(params){
            if(params.row.Store=="总计"||params.row.Store=="当前页合计"){
                return false;
            }
            var rowId = params.row.ID;
            for(var i=0;i<this.historyData.length;i++){
                if(rowId == this.historyData[i]['ID'] ){
                    this.historyData.splice(i,1);
                }
            }


            var self = this
            self.standardObject = params.row;
            var jsondata = self.standardObject;
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goSaleBack/deleteAssessById", //请求的url地址
                dataType: "json", //返回格式为json
                async:false,
                data: data_json, //参数值
                type: "POST", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                success: function(data) {
                    if(data.status=="1"){

                        toastr.success('删除成功');
                    }else{
                        toastr.error('删除失败，请刷新页面重试');
                    }
                    // location.reload();
                },
            });
            this.querytable(1,99999999);
        },
        doEditData () {
            var self = this
            var jsondata = self.standardObject;
            if(testZhengLiang(jsondata.FinishRate)==false){
                toastr.warning("完成率必须为两位小数点以内小于等于100的正数");
                return false;
            }
            var data_json = JSON.stringify(jsondata)
            $.ajax({
                url: "./goSaleBack/updateAssessById", //请求的url地址
                dataType: "json", //返回格式为json
                async:false,
                data: data_json, //参数值
                type: "POST", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                success: function(data) {
                    if(data.status=="1"){

                        toastr.success('保存成功');
                    }
                    // location.reload();
                },
                error: function() {
                    toastr.error('保存失败，请重新保存');
                    return false;
                },
            });
            this.querytable(1,99999999);
        },
        //指定行列样式
        rowClassName (row, index) {
            if (row.Store=="总计"||row.Store=="当前页合计") {
                $("#shanchubutton").attr("style","display:none");
                $("#xiugaibutton").attr("style","display:none");
            }
            return '';
        },
        inportexcel: function() {
            var AssessDateStart = $("#time1").val();
            if(AssessDateStart==""||AssessDateStart==null){
                AssessDateStart = "";
            }
            var AssessDateEnd = $("#time2").val();
            if(AssessDateEnd==""||AssessDateEnd==null){
                AssessDateEnd = "";
            }
            var Store = $("#StoreSelect").val();
            if(Store==""||Store==null){
                Store = "";
            }
            var Post = $("#PostSelect").val();
            if(Post==""||Post==null){
                Post = "";
            }
            var SalerID = $("#SaleManSelect").val();
            if(SalerID==""||SalerID==null){
                SalerID = "";
            }
            var CID = $("#CategorySelect").val();
            if(CID==""||CID==null){
                CID = "";
            }
            // var jsondata={"AssessDate":AssessDate,"Store": Store,"Post":Post,"SalerID":SalerID,"CID":CID};
            // var data_json = JSON.stringify(jsondata);
            window.location.href="./goSaleBack/CreateAssessExcel?AssessDateStart="+AssessDateStart+"&AssessDateEnd="+AssessDateEnd+"&Store="+encodeURI(encodeURI(Store))+"&Post="+encodeURI(encodeURI(Post))+"&SalerID="+encodeURI(encodeURI(SalerID))+"&CID="+encodeURI(encodeURI(CID));
        }

    }
})



//两位小数点正数
function testZhengLiang(num) {
    if(!isNaN(num)){
        num = num.toString();
        var dot = num.indexOf(".");
        if(dot != -1){
            var dotCnt = num.substring(dot+1,num.length);
            if(dotCnt.length > 2){
                return false;
            }
        }
    }else{
        return false;
    }
    if(num<0||num>100){
        return false;
    }
    return true;
}